package demo.spring.framework.context.annotation;

import java.util.Arrays;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment;

import demo.db.dynamic.config.DsConfig;

public class PropertySourceDemo {

    /**
     * 用法一：@PropertySource +
     * Environment，通过@PropertySource注解将properties配置文件中的值存储到Spring的 Environment中，
     * Environment接口提供方法去读取配置文件中的值，参数是properties文件中定义的key值。
     */
    @Test
    public void test1() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(DsConfig.class);
        context.refresh();
        Environment environment = context.getBean(Environment.class);
        System.err.println(environment.getProperty("jdbc.url"));
        System.err.println("ActiveProfiles = " + Arrays.asList(environment.getActiveProfiles()));
        System.err.println("DefaultProfiles = " + Arrays.asList(environment.getDefaultProfiles()));
        context.close();
    }

    /**
     * 用法2：@PropertySource（PropertySourcesPlaceholderConfigurer）+@Value
     */
    @Test
    public void test2() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(DsConfig.class);
        context.refresh();
        DsConfig environment = context.getBean(DsConfig.class);
        System.err.println(environment.getJdbcUrl());
        context.close();
    }
}
